<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class ArtistController extends Zend_Controller_Action
{
    public function listAllArtistsAction()
    {
    }

    public function artistaffiliatecontentAction()
    {
    }
    /**
    * Artist Profile
    *
    */
    public function profileAction()
    {
    }

    public function newAction()
    {
//        $genres =array("Electronic",
//                        "Country",
//                        "Rock",
//                        "R & B",
//                        "Hip-Hop",
//                        "Heavy-Metal",
//                        "Alternative Rock",
//                        "Christian",
//                        "Jazz",
//                        "Pop");
//
//        $this->view->genres = $genres;
        $this->view->form = $this->getAddArtistForm();


    }

    public function newsAction()
    {
//        $artists = array("Thievery Corporation",
//        "The Eagles",
//        "Elton John");
//        //Set the view variables
//        $this->view->artists = $artists;
//        //Find the view in our new location
//        $this->view->setScriptPath("../application/views/scripts/");
//        $this->render("/news");
        $this->view->form = $this->getAddArtistForm();


    }

//    public function saveArtistAction()
//    {
//        $artistName = $this->_request->getPost("artistName");
//        $genre = $this->_request->getPost("genre");
//        $rating = $this->_request->getPost("rating");
//        $isFav = $this->_request->getPost("isFav");
//
//    }
    public function saveArtistAction()
    {
//        $artistName = $this->_request->getPost("artistName");
//        $genre = $this->_request->getPost("genre");
//        $rating = $this->_request->getPost("rating");
//        $isFav = $this->_request->getPost("isFav");
//
////        $this->view->setEscape("strip_tags");
////        $artistName = $this->view->escape($artistName);
////        $genre = $this->view->escape($genre);
////        $rating = $this->view->escape($rating);
////        $isFav = $this->view->escape($isFav);
//
//        require '../application/models/utils/Escape.php';
//        $escapeObj = new Escape();
//        $this->view->setEscape(array($escapeObj, "doEnhancedEscape"));
//        $artistName = $this->view->escape($artistName);
//        $genre = $this->view->escape($genre);
//        $rating = $this->view->escape($rating);
//        $isFav = $this->view->escape($isFav);

        //Create instance of artist form.
        $form = $this->getAddArtistForm();
        //Check if there were no errors
        if($form->isValid($_POST))
        {
            //Initialize the variables
            $artistName = $form->getValue('artistName');
            $genre = $form->getValue('genre');
            $rating = $form->getValue('rating');
            $isFav = $form->getValue('isFav');
            //Set the temporary account id to use.
            $userId = 1 ;
            try
            {
                //Create a db object
                require_once "../application/models/Db/Db_Db.php";
                $db = Db_Db::conn();
                //Initialize data to save into DB
                $artistData = array("artist_name" => $artistName,"genre"=> $genre,"created_date" =>new Zend_Db_Expr("NOW()"));
                //Insert the artist into the Db
                $db->insert('artist', $artistData);
                //Fetch the artist id

                $artistId = $db->lastInsertId();
                
                //Initialize data for the account artists table

                $accountArtistData = array("account_id" => $userId,"artist_id" => $artistId,"rating"=> $rating
                                            ,"is_fav"=> $isFav,"created_date" =>new Zend_Db_Expr("NOW()"));
                //Insert the data.
                $db->insert('accounts_artists', $accountArtistData);


            }
            catch(Zend_Db_Exception $e)
            {
                echo $e->getMessage();
            }
        }
        else
        {
            echo "ESTO ESTA MAL <br>";
            echo $form->getValue('artistName')."<br>";
            echo $form->getValue('genre')."<br>";
            echo $form->getValue('rating')."<br>";
            echo $form->getValue('isFav')."<br>";
            $this->view->errors = $form->getMessages();
            $this->view->form = $form;
        }

    }

//    public function removeAction()
//    {
//        $artists = array('ColdPlay','Nirvana','AudioSlave');
//        $this->view->totalArtist = count($artists);
//        $this->view->artists = $artists;
//    }

    public function removeAction()
    {
//        $artists = array('ColdPlay','Nirvana','AudioSlave');
        $artists = array(
                        array("name"=>"ColdPlay","rating"=>"5"),
                        array("name"=>"Nirvana","rating"=>"4"),
                        array("name"=>"AudioSlave","rating"=>"3"),
                        array("name"=>"Incubus","rating"=>"2"),
                        array("name"=>"Limp Bizket","rating"=>"1")
                        );
//
//        $totalOfArtistis = array("totalArtistis"=>  count($artists));
//        $this->view->assign("artists",$artists);
//        $this->view->assign($totalOfArtistis);
//        $totalNumberOfArtists = array("totalArtist"=>  count($artists));
        $artistObj = new stdClass();
        $artistObj->artists = $artists;
        $this->view->assign((array)$artistObj);
        $totalNumberOfArtists = array("totalArtist"=>  count($artists));
        $this->view->assign((array)$artistObj);
        
        $this->view->assign($totalNumberOfArtists);

    }

    private function getAddArtistForm()
    {
        $form = new Zend_Form();
        $form->setAction("save-artist");
        $form->setMethod("post");
        $form->setName("addartist");
        //Create artist name text field.
        $artistNameElement = new Zend_Form_Element_Text('artistName');
        $artistNameElement->setLabel("Artist Name:");
        //Create genres select menu
        $genres = array("multiOptions" => array(    "electronic"=> "Electronic",
                                                    "country"=> "Country",
                                                    "rock"=> "Rock",
                                                    "r_n_b"=> "R & B",
                                                    "hip_hop"=> "Hip-Hop",
                                                    "heavy_metal" => "Heavy-Metal",
                                                    "alternative_rock" => "Alternative Rock",
                                                    "christian"=> "Christian",
                                                    "jazz"=> "Jazz",
                                                    "pop"=> "Pop",
                                                    "pop"=> "Pop"
                                                )
                        );
        $genreElement = new Zend_Form_Element_Select('genre', $genres);
        $genreElement->setLabel("Genre:");
        $genreElement->setRequired(true);
        //Create favorite radio buttons.
        $favoriteOptions = array("multiOptions" => array("1" => "si",
                                                         "0" => "no"
                                                        )
                                );
        $isFavoriteListElement = new Zend_Form_Element_Radio('isFav',$favoriteOptions);
        $isFavoriteListElement->setLabel("Add to Favorite List:");
        $isFavoriteListElement->setRequired(true);
        //Create Rating raio button
        $ratingOptions = array("multiOptions" => array("1" => "1",
                                                        "2" => "2",
                                                        "3" => "3",
                                                        "4" => "4",
                                                        "5" => "5"
                                                        )
                                );
        $ratingElement = new Zend_Form_Element_Radio('rating', $ratingOptions);
        $ratingElement->setLabel("Rating:");
        $ratingElement->setRequired(true)->addValidator(new Zend_Validate_Alnum(false));
        //Create submit button
        $submitButton = new Zend_Form_Element_Submit("submit");
        $submitButton->setLabel("Add Artist");
//        $submitButton  = $form->getElement('submit');
      
        //Add Elements to form
        $form->addElement($artistNameElement);
        $form->addElement($genreElement);
        $form->addElement($isFavoriteListElement);
        $form->addElement($ratingElement);
        $form->addElement($submitButton);
        
        return $form;
    }

    /**
    * Test - Object Oriented Select Statement
    */
    public function testoostatementAction()
    {
        //Create DB object
        require_once "../application/models/Db/Db_Db.php";
        $db = Db_Db::conn();
        //Create the statement
        //Select * FROM `artists`;
        $select = new Zend_Db_Select($db);
        $columns = array('id', 'artist_name', 'genre');
        $statement = $select->from('artists',$columns);
        //Compare Statement
        echo $statement->__toString();
        //Supress the View
        $this->_helper->viewRenderer->setNoRender();
    }

    /**
    * Display all the Artists in the system.
    */
//    public function listAction()
//    {
//        //Create a sample array of artist
//        $artist = array("Underworld", "Groove Armada", "Daft Punk",
//                        "Paul Oakenfold", "MC Chris", "Ramones",
//                        "The Beatles", "The Mamas and the Papas",
//                        "Jimi Hendrix");
//        //Initialize the Zend_Paginator
//        $paginator = Zend_Paginator::factory($artist);
//        $currentPage = 1;
//        //Check if the user is not on page 1
//
//        $i = $this->_request->getQuery('i');
//        if(!empty($i))
//        { //Where i is the current page
//            $currentPage = $this->_request->getQuery('i');
//        }
//        //Set the properties for the pagination
//        $paginator->setItemCountPerPage(2);
//        $paginator->setPageRange(3);
//        $paginator->setCurrentPageNumber($currentPage);
//        $this->view->paginator = $paginator;
//    }

    /**
    * Display all the Artists in the system.
    */
    public function listAction()
    {
        $currentPage = 1;
        //Check if the user is not on page 1
        $i = $this->_request->getQuery('i');
        if(!empty($i))
        { //Where i is the current page
            $currentPage = $this->_request->getQuery('i');
        }
        //Create Db object
        require_once "../application/models/Db/Db_Db.php";
        $db = Db_Db::conn();
        //Create a Zend_Db_Select object
        $sql = new Zend_Db_Select($db);
        //Define columns to retrieve as well as the table.
        $columns = array("id", "artist_name");
        $table = array("artist");
        //SELECT `artists`.`id`, `artists`.`artist_name` FROM `artists`
        $statement = $sql->from($table, $columns);
        //Initialize the Zend_Paginator
        $paginator = Zend_Paginator::factory($statement);
        //Set the properties for the pagination
        $paginator->setItemCountPerPage(10);
        $paginator->setPageRange(3);
        $paginator->setCurrentPageNumber($currentPage);
        $this->view->paginator = $paginator;
    }


}

?>
